package com.cloud.admin.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.cloud.admin.mapper.SysOperLogMapper;
import com.cloud.admin.model.entity.SysOperLog;
import com.cloud.admin.service.SysOperLogService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 操作日志记录
 *
 * @author douhaichao code generator
 * @date 2019-07-09 10:26:51
 */
@Service
@AllArgsConstructor
public class SysOperLogServiceImpl extends ServiceImpl<SysOperLogMapper, SysOperLog> implements SysOperLogService {

    /**
     * 根据对象属性查找满足条件的第一条数据     *      * @return
     */
    @Override
    public SysOperLog selectOne(SysOperLog req) {
        QueryWrapper queryWrapper = new QueryWrapper<SysOperLog>();
        queryWrapper.setEntity(req);
        return baseMapper.selectOne(queryWrapper);
    }

    /**
     * 根据条件查询所有
     */
    @Override
    public List<SysOperLog> selectAll(SysOperLog req) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.setEntity(req);
        return baseMapper.selectList(queryWrapper);
    }

    /**
     * 根据对象查询所有分页数据(精确查询)     *      * @return
     */
    @Override
    public IPage<SysOperLog> selectPage(Page page, SysOperLog req) {
        IPage<SysOperLog> list = baseMapper.selectPage(
                page,
                new QueryWrapper<SysOperLog>().setEntity(req));
        return list;
    }

    /**
     * 根据对象查询所有分页数据(模糊查询)     *      * @return
     */
    @Override
    public IPage<SysOperLog> selectPageByLike(Page page, SysOperLog req) {
        QueryWrapper queryWrapper = new QueryWrapper();

        if (null != req.getOperId()) {
            queryWrapper.eq("oper_id", req.getOperId());
        }

        if (!StrUtil.isEmpty(req.getTitle())) {
            queryWrapper.like("title", req.getTitle());
        }

        if (null != req.getBusinessType()) {
            queryWrapper.eq("business_type", req.getBusinessType());
        }

        if (!StrUtil.isEmpty(req.getMethod())) {
            queryWrapper.like("method", req.getMethod());
        }

        if (null != req.getOperatorType()) {
            queryWrapper.eq("operator_type", req.getOperatorType());
        }

        if (!StrUtil.isEmpty(req.getOperName())) {
            queryWrapper.like("oper_name", req.getOperName());
        }

        if (!StrUtil.isEmpty(req.getDeptName())) {
            queryWrapper.like("dept_name", req.getDeptName());
        }

        if (!StrUtil.isEmpty(req.getOperUrl())) {
            queryWrapper.like("oper_url", req.getOperUrl());
        }

        if (!StrUtil.isEmpty(req.getOperIp())) {
            queryWrapper.like("oper_ip", req.getOperIp());
        }

        if (!StrUtil.isEmpty(req.getOperLocation())) {
            queryWrapper.like("oper_location", req.getOperLocation());
        }

        if (!StrUtil.isEmpty(req.getOperParam())) {
            queryWrapper.like("oper_param", req.getOperParam());
        }

        if (null != req.getStatus()) {
            queryWrapper.eq("status", req.getStatus());
        }

        if (!StrUtil.isEmpty(req.getErrorMsg())) {
            queryWrapper.like("error_msg", req.getErrorMsg());
        }

        if (null != req.getOperTime()) {
            queryWrapper.eq("oper_time", req.getOperTime());
        }
        IPage<SysOperLog> list = baseMapper.selectPage(page, queryWrapper);
        return list;
    }

    /**
     * 根据条件统计数据量     *      * @return
     */
    @Override
    public int count(SysOperLog req) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.setEntity(req);
        return baseMapper.selectCount(queryWrapper);
    }
}
